package com.locationhistory.dao.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.locationhistory.bean.LocationHistory;
import com.locationhistory.dao.LocationDAO;
import com.locationhistory.utils.Constants;

public class LocationDAOImpl extends DatabaseHandler implements LocationDAO {

	public LocationDAOImpl(Context context) {
		super(context);
	}

	@Override
	public void saveLocation(LocationHistory locationHistory) {
		//open Database
		super.checkAndCopyDatabase();
		super.openDatabase();
		        
        ContentValues values = new ContentValues();
        values.put(Constants.Database.KEY_LOCATION_LOCATION, locationHistory.getLocation());
        values.put(Constants.Database.KEY_LOCATION_UPDATED_TIME, locationHistory.getUpdatedTime());
 
        // Inserting Row
        myDatabase.insert(Constants.Database.TABLE_LOCATION, null, values);
        myDatabase.close(); // Closing database connection
		
	} // end saveLocation(LocationHistory locationHistory)

	@Override
	public void updateLocation(LocationHistory locationHistory) {
		//open Database
		super.checkAndCopyDatabase();
		super.openDatabase();
		        
        ContentValues values = new ContentValues();
        values.put(Constants.Database.KEY_LOCATION_LOCATION, locationHistory.getLocation());
        values.put(Constants.Database.KEY_LOCATION_UPDATED_TIME, locationHistory.getUpdatedTime());
 
        // updating Row
        myDatabase.update(Constants.Database.TABLE_LOCATION, values, Constants.Database.KEY_DEVICE_ID + " = ?",
                new String[] { String.valueOf(locationHistory.getId()) });
        myDatabase.close(); // Closing database connection
	} // end updateLocation(LocationHistory locationHistory)

	@Override
	public LocationHistory getLocationByAddress(String address) {
		checkAndCopyDatabase();
        openDatabase();
		 
        Cursor cursor = myDatabase.query(Constants.Database.TABLE_LOCATION, 
        		new String[] { Constants.Database.KEY_LOCATION_ID, 
        						Constants.Database.KEY_LOCATION_LOCATION, 
        						Constants.Database.KEY_LOCATION_UPDATED_TIME,
        						}, 
        						
        Constants.Database.KEY_LOCATION_LOCATION + "=?", new String[] { String.valueOf(address) }, null, null, null, null);
       
        LocationHistory location = null;
        if (cursor != null) {
        	if (cursor.moveToFirst()) {
        		cursor.moveToFirst();
        		
        		location = new LocationHistory();
            	location.setId(Integer.parseInt(cursor.getString(0)));
            	location.setLocation(cursor.getString(1));
            	location.setUpdatedTime(cursor.getString(2));
        	}
        
        } // end if
            
        myDatabase.close();
        
        // return device
        return location;
	} // end getLocationByAddress(String address)

	@Override
	public List<LocationHistory> getLocationList() {
		checkAndCopyDatabase();
        openDatabase();
        
        // Select All Query
        String selectQuery = "SELECT  * FROM " +  Constants.Database.KEY_LOCATION_LOCATION;
        List<LocationHistory> locationList = new ArrayList<LocationHistory>();
		 
        Cursor cursor = myDatabase.rawQuery(selectQuery, null);
        LocationHistory location = null;
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
            	location = new LocationHistory();
            	location.setId(Integer.parseInt(cursor.getString(0)));
            	location.setLocation(cursor.getString(1));
            	location.setUpdatedTime(cursor.getString(2));
                // Adding contact to list
            	locationList.add(location);
            } while (cursor.moveToNext());
        }
        
        return locationList;
	} // end  getLocationList()

}
